﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings.Commands
{
    public class DeleteSchema:GenericCommand
    {
        public DeleteSchema(DataFactory factory):base(factory)
        {
            const string sql = @"
delete from {$ns}_settings_values WHERE schema_id 
in 
    (select id from {$ns}_settings_schema where application_id 
        in 
            (select id from {$ns}_settings_applications WHERE 
    application= @application 
    AND version= @version)
    )

delete from {$ns}_settings_user WHERE schema_id 
in 
    (select id from {$ns}_settings_schema where application_id 
        in 
            (select id from {$ns}_settings_applications WHERE 
    application= @application 
    AND version= @version)
    )


delete from {$ns}_settings_schema where application_id 
        in 
            (select id from {$ns}_settings_applications WHERE 
    application= @application 
    AND version= @version)


delete from {$ns}_settings_applications WHERE 
    application= @application 
    AND version= @version
";
            ComposeSql(sql);
            AddParameter("@application", DbType.String);
            AddParameter("@version", DbType.String);
        }

        public void Delete(
            string application,
            Version version
            )
        {
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            if (version == null)
                throw new ArgumentException("Missing version", "version");

            command.Parameters["@application"].Value = application;
            command.Parameters["@version"].Value = version.ToString();
            ExecuteNonQuery();
        }
    }
}
